﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FProductCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //co 3 cachs: dung cau lenh sql lay id duoi dang chuoi ve tach lay so roi cong them 1
        //hoac dung Delegate
        //hoac cach nay     
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Button2")
        {
            int[] arr = new int[10];
            Random ran = new Random();

            string ProductID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
            Label UnitPriceLabel = ((Label)e.Item.FindControl("UnitPriceLabel"));
            Label QuantityInStockLabel = ((Label)e.Item.FindControl("QuantityInStockLabel"));
            TextBox Quan = ((TextBox)e.Item.FindControl("txtbuyquantity"));
            Label Label1 = ((Label)e.Item.FindControl("Label1"));

            ShoppingCart sh = new ShoppingCart();
            ShoppingCartBOL sBOL = new ShoppingCartBOL();
            try
            {
                for (int i = 0; i < arr.Length;i++)
                {
                    int n1 = ran.Next(0, arr.Length);
                    for (int j = 0; j < arr.Length;j++)
                    {
                        int n2 = ran.Next(0, arr.Length);
                        for (int k = 1; k < arr.Length;k++)
                        {
                            int n3 = ran.Next(1, arr.Length);

                            sh.ShoppingID = "S" + n1.ToString() + n2.ToString() + n3.ToString();
                            sh.ProID = ProductID;
                            int test = Convert.ToInt32(Quan.Text);
                            sh.Quantity = test;
                            sh.TotalPrice = Convert.ToDouble(UnitPriceLabel.Text) * (Convert.ToDouble(test));
                            sh.Identification = "173074915";
                            sh.DateShopping = DateTime.Now;
                            sh.Status = "Unpaid";
                            int QuantityInStock = Convert.ToInt32(QuantityInStockLabel.Text);
                            if (test > QuantityInStock)
                            {
                                Label1.Text = "Please enter a smaller quantity to buy!";
                                break;
                            }
                            else
                            {
                                int result = sBOL.insertShoppingcart(sh);
                                if (result > 0)
                                {
                                    Label1.Text = "Thank you!";

                                    Product p = new Product();
                                    ProductBOL pBOL = new ProductBOL();

                                    p.ProductID = sh.ProID;
                                    p.QuantityInStock = QuantityInStock - test;
                                    pBOL.updateProductQuantityInStock(p);

                                    break;
                                }
                                else
                                    Label1.Text = "Sorry-Try Again Please.....!";
                            }
                        }
                        break;
                    }
                    break;
                }
                
            }
            catch (Exception)
            {
                Label1.Text= "Sorry-Try Again Please!";
            }
            finally
            {
                sh = null;
                sBOL = null;
            }
        }
    }
}